package com.engineering.elink.service.impl;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.engineering.elink.co.UserCo;
import com.engineering.elink.common.Constant;
import com.engineering.elink.common.ResultType;
import com.engineering.elink.dao.CertificationMapper;
import com.engineering.elink.dao.CompanyMapper;
import com.engineering.elink.dao.FeedbackMapper;
import com.engineering.elink.dao.TokenAuthenticationMapper;
import com.engineering.elink.dao.UserMapper;
import com.engineering.elink.domain.Certification;
import com.engineering.elink.domain.Feedback;
import com.engineering.elink.domain.User;
import com.engineering.elink.service.RedisService;
import com.engineering.elink.service.UserService;
import com.engineering.elink.service.VerificationService;
import com.engineering.elink.util.MapConvert;
import com.engineering.elink.util.global.TokenExpireTime;
import com.engineering.elink.util.token.MD5Util;
import com.engineering.elink.util.token.TokenGenerator;
import com.engineering.elink.util.tool.UUIDUtil;
import com.engineering.elink.util.ucpaas.Rest;
import com.engineering.elink.util.verify.VerifyCodeUtils;

@Service("userService")
public class UserServiceImpl implements UserService {
	@Autowired
	private RedisService redisService;
	@Autowired
	private UserMapper userMapper;
	@Autowired
	private CompanyMapper companyMapper;
	@Autowired
	private TokenAuthenticationMapper tokenAuthenticationMapper;
	@Autowired
	private VerificationService verificationService;
	@Autowired
	private FeedbackMapper feedbackMapper;
	@Autowired
	private CertificationMapper certificationMapper;

	/**
	 * 注册请求 1>查询缓存的验证码令牌 如果存在 2>写入用户信息 3>生成authToken 并存入缓存中 4>存入数据库做持久化
	 */
	@SuppressWarnings("unchecked")
	@Transactional
	@Override
	public Map<String, Object> reg(UserCo userCo) {
		Map<String, Object> map = new HashMap<String, Object>();
		// 查询缓存并验证
		String verifyToken = "";
		String verifyCode = userCo.getVerifyCode();
		Integer transactionalStep = 0;
		Map<String, Object> mapInRedis = (Map<String, Object>) redisService
				.getObjectBySeed(Constant.verifyCodeCache, verifyToken);
		if (mapInRedis != null) {
			// 这里存档的是手机号和验证码信息 为了防止用户提交注册数据时修改手机号码
			map = (Map<String, Object>) mapInRedis.get("object");
			if (map != null && map.containsKey("verifyCode")
					&& map.containsKey("phone")) {
				// 获取发送短信验证码时存入的手机号和验证码
				String verifyCodeInCache = (String) map.get("verifyCode");
				String phoneInCache = (String) map.get("phone");
				System.out.println("verifyCode=" + verifyCode);
				System.out.println("verifyToken=" + verifyToken);
				System.out.println("verifyCodeInCache=" + verifyCodeInCache);
				System.out.println("phoneInCache=" + phoneInCache);
				System.out.println("userCo=" + userCo.toString());
				System.out.println("userCo.getPhone()=" + userCo.getPhone());
				System.out.println("verifyCode=" + verifyCode);
				if (verifyCodeInCache != null && phoneInCache != null
						&& verifyCodeInCache.equals(verifyCode)
						&& phoneInCache.equals(userCo.getPhone())) {
					redisService.delObjectBySeed(Constant.verifyCodeCache,
							verifyToken);
					transactionalStep++;
				}
			}
		}
		if (transactionalStep != 1) {
			System.out.println("####################1");
			return null;
		}
		// 生成新用户数据 用户ID和密码
		userCo.setUserId(UUIDUtil.getUUID());
		userCo.setPasswd(MD5Util.getMD5String(userCo.getPasswd()));
		userCo.setCreateTime(new Date());
		if (userMapper.insertSelective(userCo) == 1) {
			transactionalStep++;
			System.out.println("####################2");
		}
		if (transactionalStep != 2) {
			return null;
		}
		// 将用户令牌存入缓存
		String seed = TokenGenerator.getSeed(userCo.getUserId());
		String authToken = TokenGenerator.getToken();
		map.put("authToken", authToken);
		map.put("userId", userCo.getUserId());
		System.out.println("####################3");
		redisService.putSeedObjectMap(Constant.tokenAuthenticationCache, seed,
				map, Constant.validTime);
		// 将用户令牌存入持久化
		map.clear();
		map.put("seed", seed);
		map.put("userId", userCo.getUserId());
		map.put("expireTimeDiff", TokenExpireTime.AuthTokenTTL);
		map.put("token", authToken);
		if (tokenAuthenticationMapper.insertAuthToken(map) == 1) {
			System.out.println("####################4");
			transactionalStep++;
		}
		if (transactionalStep != 3) {
			System.out.println("####################5");
			return null;
		}
		map.clear();
		map.put("seed", seed);
		map.put("authToken", authToken);
		return map;
	}

	@Override
	public boolean checkPhone(String phone) {
		return userMapper.selectByPhone(phone) == null ? true : false;
	}

	/**
	 * 生成验证码并赋予时效
	 */
	@Override
	public void generateVerifyCode(int dbNum, String verifyToken,
			String verifyCode, int validTime) {
		redisService
				.putSeedObjectMap(dbNum, verifyToken, verifyCode, validTime);
	}

	/**
	 * 校验验证码
	 */
	@Override
	public boolean checkVerifyCode(int dbNum, String verifyToken,
			String verifyCode) {
		@SuppressWarnings("unchecked")
		Map<String, Object> mapInRedis = (Map<String, Object>) redisService
				.getObjectBySeed(dbNum, verifyToken);
		if (mapInRedis != null) {
			// 这里存档的是手机号和验证码信息 为了防止用户提交注册数据时修改手机号码
			if (verifyCode.equals(mapInRedis.get("object"))) {
				redisService.delObjectBySeed(dbNum, verifyToken);
				return true;
			}
		}
		return false;
	}

	@Override
	public void sendPhoneVerifyCode(String phone, String phoneVerifyCode) {
		// 这里写发送验证码的过程
		try {
			Rest.sendMessage(phone, phoneVerifyCode);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public void saveCachePhoneVerifyCode(String verifyToken, String verifyCode,
			String phone) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("phone", phone);
		map.put("verifyCode", verifyCode);
		redisService.putSeedObjectMap(Constant.verifyCodeCache, verifyToken,
				map, Constant.validTime);
	}

	@Override
	public boolean checkEmail(String email) {
		return userMapper.selectByEmail(email) == null ? true : false;
	}

	@SuppressWarnings("unchecked")
	@Override
	public String refreshAuthToken(String userId) {
		String seed = TokenGenerator.getSeed(userId);
		String authToken = TokenGenerator.getToken();
		// 实际上最终的token是否变化 如果变化返回null 否则返回更新后的token
		String newAuthToken = null;
		Integer newExpireTimeDiff = TokenExpireTime.AuthTokenTTL;
		// 更新数据库身份令牌 如果数据库中有令牌 则比较时效 如果过期则更新
//		TokenAuthentication tokenAuthentication = tokenAuthenticationMapper
//				.selectBySeed(seed);
//		System.out.println("############################" + 1);
//		if (tokenAuthentication != null) {
//			System.out.println("############################" + 2);
//			// 如果数据库token失效了
//			if (tokenAuthentication.getExpireTime().getTime() < new Date()
//					.getTime()) {
//				System.out.println("############################" + 3);
//				Map<String, Object> map = new HashMap<String, Object>();
//				map.put("expireTimeDiff", TokenExpireTime.AuthTokenTTL);
//				map.put("authToken", authToken);
//				map.put("seed", seed);
//				tokenAuthenticationMapper.updateAuthToken(map);
//				newAuthToken = authToken;
//			} else {
//				System.out.println("############################" + 4);
//				// 如果数据库中的token还有效 那么缓存中用数据中的token 时效为数据库时间-当前时间
//				// 如果数据库中的token无效了 那么缓存中用生成的新token 时效为新的时间
//				authToken = tokenAuthentication.getToken();
//				newAuthToken = authToken;
//				newExpireTimeDiff = (int) ((tokenAuthentication.getExpireTime()
//						.getTime() - new Date().getTime()) / 1000);
//			}
//		}
		// 更新缓存身份令牌 如果缓存中有令牌则一定不会过期 如果没有则生成新的
		Map<String, Object> mapInRedis = (Map<String, Object>) redisService
				.getObjectBySeed(Constant.tokenAuthenticationCache, seed);
		System.out.println("############################" + 5);
		Map<String, Object> map = new HashMap<String, Object>();
		System.out.println("#MAP####="+map.toString());
		if (mapInRedis == null || mapInRedis.get("object") == null) {
			System.out.println("############################" + 6);
			newAuthToken = authToken;
			map.put("userId", userId);
			map.put("authToken", authToken);
			System.out.println("authToken=" + authToken);
			System.out.println("newExpireTimeDiff=" + newExpireTimeDiff);
			System.out.println("############################" + 7);
			redisService.putSeedObjectMap(Constant.tokenAuthenticationCache,
					seed, map, Constant.validTime);
		} else {
//			map.put("userId", userId);
//			map.put("authToken", authToken);
//			newAuthToken = authToken;
//			redisService.putSeedObjectMap(Constant.tokenAuthenticationCache,
//					seed, map, Constant.validTime);
			System.out.println("#####################"+(String)(((Map<String,Object>) mapInRedis.get("object")).get("authToken")));
			newAuthToken = (String)(((Map<String,Object>) mapInRedis.get("object")).get("authToken"));
		}
		return newAuthToken;
	}

	@SuppressWarnings("unchecked")
	@Override
	public boolean forgetPassByPhone(String phone, String verifyToken,
			String verifyCode) {
		Map<String, Object> map = new HashMap<String, Object>();
		Map<String, Object> mapInRedis = (Map<String, Object>) redisService
				.getObjectBySeed(Constant.verifyCodeCache, verifyToken);
		if (mapInRedis != null) {
			// 这里存档的是手机号和验证码信息 为了防止用户提交注册数据时修改手机号码
			map = (Map<String, Object>) mapInRedis.get("object");
			if (map != null && map.containsKey("verifyCode")
					&& map.containsKey("phone")) {
				// 获取发送短信验证码时存入的手机号和验证码
				String verifyCodeInCache = (String) map.get("verifyCode");
				String phoneInCache = (String) map.get("phone");
				if (verifyCodeInCache != null && phoneInCache != null
						&& verifyCodeInCache.equals(verifyCode)
						&& phoneInCache.equals(phone)) {
					redisService.delObjectBySeed(Constant.verifyCodeCache,
							verifyToken);
				}
			}
		}
		return false;
	}

	@Override
	public boolean modifyPasswd(String userId, String passwd, String newPasswd) {
		Map<String, Object> map = userMapper.selectInfo(userId,MD5Util.getMD5String(passwd));
		if (map != null && map.containsKey("passwd")) {
				UserCo userCo = new UserCo();
				userCo.setUserId(userId);
				userCo.setPasswd(MD5Util.getMD5String(newPasswd));
				userMapper.updateInfo(userCo);
				return true;
		}
		return false;
	}

	@Override
	public void checkAuthWithVerifyCode(int checkMethod, String verifyToken,
			String verifyCode) {
		Map<String, Object> map = new HashMap<String, Object>();
		Map<String, Object> dataMap = new HashMap<String, Object>();
		dataMap.put("verifyCode", verifyCode);
		map.put("data", dataMap);
		// status == 1 发送验证码 第一步
		map.put("status", 1);
		map.put("checkMethod", checkMethod);
		map.put("type", Constant.checkAuthCache);
		redisService.putSeedObjectMap(Constant.redisCommonCache, verifyToken,
				map, Constant.checkAuthCacheOneStepValidTime);
	}

	@SuppressWarnings("unchecked")
	@Override
	public boolean checkAuthWithSendCode(String phoneOrEmail,
			String verifyToken, String verifyCode,HttpServletRequest request) {
		Map<String, Object> map = (Map<String, Object>) redisService.getObjectBySeed(Constant.redisCommonCache, verifyToken);
		if (map == null|| !map.containsKey("object")	|| map.get("object") == null || !((Map<String, Object>) map.get("object")).containsKey("status")
				|| !((Map<String, Object>) map.get("object"))	.containsKey("checkMethod")
				|| !((Map<String, Object>) map.get("object")).containsKey("type")
				|| !((Map<String, Object>) map.get("object"))	.containsKey("data")) {
			return false;
		}
		map = (Map<String, Object>) map.get("object");
		Map<String, Object> dataMap = (Map<String, Object>) map.get("data");
		int status = (int) map.get("status");
		int type = (int) map.get("type");
		int checkMethod = (int) map.get("checkMethod");
		String verifyCodeInRedis = (String) dataMap.get("verifyCode");
		System.out.println(map.toString());
		if (status == 1 && type == Constant.checkAuthCache&& verifyCode.equals(verifyCodeInRedis)) {
			System.out.println("#########################" + status);
			// 发送验证码
			verifyCode = VerifyCodeUtils.generateNumberCode(4);
			if (checkMethod == Constant.checkMethodByPhone) {
				if (userMapper.selectByPhone(phoneOrEmail) == null) {
					return false;
				} else {
					verificationService.sendPhoneCode(phoneOrEmail, verifyCode);
				}
			} else if (checkMethod == Constant.checkMethodByEmail) {
				if (userMapper.selectByEmail(phoneOrEmail) == null) {
					return false;
				} else {
					String redirectUrl = (String) request.getAttribute("redirectUrl");
//					String redirectUrl = (String) LocalUtil.get();
					verificationService.sendEmail(phoneOrEmail, redirectUrl
							+ "?email=" + phoneOrEmail + "&verifyToken="
							+ verifyToken + "&verifyCode=" + verifyCode);
				}
			}
			// todo
			System.out.println("手机短信验证码为=" + verifyCode);
			System.out.println("手机为=" + phoneOrEmail);
			System.out.println("map=" + map);
			dataMap.put("phoneOrEmail", phoneOrEmail);
			dataMap.put("verifyCode", verifyCode);
			map.put("data", dataMap);
			// status == 2 发送短信 第二步
			map.put("status", 2);
			redisService
					.putSeedObjectMap(Constant.redisCommonCache, verifyToken,
							map, Constant.checkAuthCacheThreeStepValidTime);
			return true;
		}
		return false;
	}

	@SuppressWarnings("unchecked")
	@Override
	public boolean checkAuthWithCheckCode(String phoneOrEmail,
			String verifyToken, String verifyCode) {
		Map<String, Object> map = (Map<String, Object>) redisService
				.getObjectBySeed(Constant.redisCommonCache, verifyToken);
		if (map == null
				|| !map.containsKey("object")
				|| map.get("object") == null
				|| !((Map<String, Object>) map.get("object"))
						.containsKey("status")
				|| !((Map<String, Object>) map.get("object"))
						.containsKey("checkMethod")
				|| !((Map<String, Object>) map.get("object"))
						.containsKey("type")
				|| !((Map<String, Object>) map.get("object"))
						.containsKey("data"))
			return false;
		map = (Map<String, Object>) map.get("object");
		Map<String, Object> dataMap = (Map<String, Object>) map.get("data");
		int status = (int) map.get("status");
		int type = (int) map.get("type");
		String verifyCodeInRedis = (String) dataMap.get("verifyCode");
		String phoneOrEmailInRedis = (String) dataMap.get("phoneOrEmail");
		System.out.println(map.toString());
		System.out.println("verifyCode=" + verifyCode);
		System.out.println("phoneOrEmail=" + phoneOrEmail);
		if ((status == 2 || status == 3) && type == Constant.checkAuthCache
				&& verifyCode.equals(verifyCodeInRedis)
				&& phoneOrEmail.equals(phoneOrEmailInRedis)) {
			map.put("status", 3);
			redisService.putSeedObjectMap(Constant.redisCommonCache,
					verifyToken, map, Constant.checkAuthCacheTwoStepValidTime);
			return true;
		}
		return false;
	}

	@SuppressWarnings({ "unchecked" })
	@Override
	public boolean modifyEmailOrPhoneWithVerifyCode(int newCheckMethod,
			String verifyToken, String verifyCode) {
		Map<String, Object> map = (Map<String, Object>) redisService
				.getObjectBySeed(Constant.redisCommonCache, verifyToken);
		if (map == null
				|| !map.containsKey("object")
				|| map.get("object") == null
				|| !((Map<String, Object>) map.get("object"))
						.containsKey("status")
				|| !((Map<String, Object>) map.get("object"))
						.containsKey("checkMethod")
				|| !((Map<String, Object>) map.get("object"))
						.containsKey("type")
				|| !((Map<String, Object>) map.get("object"))
						.containsKey("data")) {
			System.out.println(11111);
			return false;
		}
		System.out.println(22222);
		map = (Map<String, Object>) map.get("object");
		Map<String, Object> dataMap = (Map<String, Object>) map.get("data");
		int status = (int) map.get("status");
		int type = (int) map.get("type");
		if ((status == 3 || status == 4)
				&& (type == Constant.checkAuthCache || type == Constant.modifyEmailOrPhoneCache)) {
			dataMap.put("verifyCode", verifyCode);
			map.put("data", dataMap);
			map.put("status", 4);
			map.put("checkMethod", newCheckMethod);
			map.put("type", Constant.modifyEmailOrPhoneCache);
			redisService.putSeedObjectMap(Constant.redisCommonCache,
					verifyToken, map,
					Constant.modifyEmailOrPhoneCacheOneStepValidTime);
			return true;
		}
		return false;
	}

	@SuppressWarnings("unchecked")
	@Override
	public boolean modifyEmailOrPhoneWithSendCode(String newPhoneOrEmail,
			String verifyToken, String verifyCode,HttpServletRequest request) {
		Map<String, Object> map = (Map<String, Object>) redisService
				.getObjectBySeed(Constant.redisCommonCache, verifyToken);
		if (map == null
				|| !map.containsKey("object")
				|| map.get("object") == null
				|| !((Map<String, Object>) map.get("object"))
						.containsKey("status")
				|| !((Map<String, Object>) map.get("object"))
						.containsKey("checkMethod")
				|| !((Map<String, Object>) map.get("object"))
						.containsKey("type")
				|| !((Map<String, Object>) map.get("object"))
						.containsKey("data")) {
			return false;
		}
		map = (Map<String, Object>) map.get("object");
		Map<String, Object> dataMap = (Map<String, Object>) map.get("data");
		int status = (int) map.get("status");
		int type = (int) map.get("type");
		String verifyCodeInRedis = (String) dataMap.get("verifyCode");
		System.out.println(map.toString());
		int checkMethod = (int) map.get("checkMethod");
		System.out.println("verifyCode=" + verifyCode);
		if ((status == 4 || status == 5)
				&& type == Constant.modifyEmailOrPhoneCache
				&& verifyCode.equals(verifyCodeInRedis)) {
			// 发送验证码
			verifyCode = VerifyCodeUtils.generateNumberCode(4);
			if (checkMethod == Constant.checkMethodByPhone) {
				if (userMapper.selectByPhone(newPhoneOrEmail) != null) {
					return false;
				} else {
					verificationService.sendPhoneCode(newPhoneOrEmail,
							verifyCode);
				}
			} else if (checkMethod == Constant.checkMethodByEmail) {
				if (userMapper.selectByEmail(newPhoneOrEmail) != null) {
					return false;
				} else {
//					String redirectUrl = (String) LocalUtil.get();
					String redirectUrl = (String) request.getAttribute("redirectUrl");
					verificationService.sendEmail(newPhoneOrEmail, redirectUrl
							+ "?email=" + newPhoneOrEmail + "&verifyToken="
							+ verifyToken + "&verifyCode=" + verifyCode);
				}
			}
			// todo
			System.out.println("手机短信验证码为=" + verifyCode);
			dataMap.put("verifyCode", verifyCode);
			dataMap.put("newPhoneOrEmail", newPhoneOrEmail);
			map.put("data", dataMap);
			// status == 2 发送短信 第二步
			map.put("status", 5);
			redisService.putSeedObjectMap(Constant.redisCommonCache,
					verifyToken, map,
					Constant.modifyEmailOrPhoneCacheTwoStepValidTime);
			return true;
		}
		return false;
	}

	@SuppressWarnings("unchecked")
	@Override
	public boolean modifyEmailOrPhoneWithCheckCode(String newPhoneOrEmail,
			String verifyToken, String verifyCode) {
		Map<String, Object> map = (Map<String, Object>) redisService
				.getObjectBySeed(Constant.redisCommonCache, verifyToken);
		if (map == null
				|| !map.containsKey("object")
				|| map.get("object") == null
				|| !((Map<String, Object>) map.get("object"))
						.containsKey("status")
				|| !((Map<String, Object>) map.get("object"))
						.containsKey("checkMethod")
				|| !((Map<String, Object>) map.get("object"))
						.containsKey("type")
				|| !((Map<String, Object>) map.get("object"))
						.containsKey("data"))
			return false;
		map = (Map<String, Object>) map.get("object");
		Map<String, Object> dataMap = (Map<String, Object>) map.get("data");
		int status = (int) map.get("status");
		int type = (int) map.get("type");
		int checkMethod = (int) map.get("checkMethod");
		String verifyCodeInRedis = (String) dataMap.get("verifyCode");
		String phoneOrEmailInRedis = (String) dataMap.get("phoneOrEmail");
		String newPhoneOrEmailInRedis = (String) dataMap.get("newPhoneOrEmail");
		System.out.println(map.toString());
		System.out.println("status=" + status);
		System.out.println("type=" + type);
		System.out.println("verifyCode=" + verifyCode);
		System.out.println("verifyCodeInRedis=" + verifyCodeInRedis);
		System.out.println("newPhoneOrEmail=" + newPhoneOrEmail);
		System.out.println("newPhoneOrEmailInRedis=" + newPhoneOrEmailInRedis);
		System.out.println(status == 5);
		System.out.println(type == Constant.modifyEmailOrPhoneCache);
		System.out.println(verifyCode.equals(verifyCodeInRedis));
		System.out.println(newPhoneOrEmail.equals(newPhoneOrEmailInRedis));
		if (status == 5 && type == Constant.modifyEmailOrPhoneCache
				&& verifyCode.equals(verifyCodeInRedis)
				&& newPhoneOrEmail.equals(newPhoneOrEmailInRedis)) {
			System.out.println("!!!!!!!!!!!!!!!");
			User newUser = null;
			User oldUser = null;
			if (checkMethod == Constant.checkMethodByPhone) {
				newUser = userMapper.selectByPhone(newPhoneOrEmailInRedis);
				oldUser = userMapper.selectByPhone(phoneOrEmailInRedis);
				if (oldUser != null && newUser == null) {
					oldUser.setPhone(newPhoneOrEmailInRedis);
					userMapper.updateByPrimaryKeySelective(oldUser);
					redisService.delObjectBySeed(Constant.redisCommonCache,
							verifyToken);
					return true;
				}
			} else if (checkMethod == Constant.checkMethodByEmail) {
				newUser = userMapper.selectByPhone(newPhoneOrEmailInRedis);
				oldUser = userMapper.selectByPhone(phoneOrEmailInRedis);
				if (oldUser != null && newUser == null) {
					oldUser.setEmail(newPhoneOrEmailInRedis);
					userMapper.updateByPrimaryKeySelective(oldUser);
					redisService.delObjectBySeed(Constant.redisCommonCache,
							verifyToken);
					return true;
				}
			}
		}
		return false;
	}

	@SuppressWarnings("unchecked")
	@Override
	public boolean modifyPasswdByEmailOrPhone(String passwd, String verifyToken) {
		Map<String, Object> map = (Map<String, Object>) redisService
				.getObjectBySeed(Constant.redisCommonCache, verifyToken);
		if (map == null
				|| !map.containsKey("object")
				|| map.get("object") == null
				|| !((Map<String, Object>) map.get("object"))
						.containsKey("status")
				|| !((Map<String, Object>) map.get("object"))
						.containsKey("checkMethod")
				|| !((Map<String, Object>) map.get("object"))
						.containsKey("type")
				|| !((Map<String, Object>) map.get("object"))
						.containsKey("data")) {
			return false;
		}
		map = (Map<String, Object>) map.get("object");
		Map<String, Object> dataMap = (Map<String, Object>) map.get("data");
		int status = (int) map.get("status");
		int type = (int) map.get("type");
		int checkMethod = (int) map.get("checkMethod");
		String phoneOrEmailInRedis = (String) dataMap.get("phoneOrEmail");
		if (status == 3 && type == Constant.checkAuthCache) {
			User user = null;
			if (checkMethod == Constant.checkMethodByPhone) {
				user = userMapper.selectByPhone(phoneOrEmailInRedis);
			} else if (checkMethod == Constant.checkMethodByEmail) {
				user = userMapper.selectByEmail(phoneOrEmailInRedis);
			}
			if (user == null || passwd.equals(user.getPasswd())) {
				return false;
			}
			user.setPasswd(MD5Util.getMD5String(passwd));
			userMapper.updateByPrimaryKeySelective(user);
			redisService
					.delObjectBySeed(Constant.redisCommonCache, verifyToken);
			return true;
		}
		return false;
	}

	@Override
	public void addFeedBack(Feedback feedBack) {
		feedBack.setTime(new Date());
		feedbackMapper.insertSelective(feedBack);
	}

	@Override
	public boolean logOut(String userId) {
		redisService.delObjectBySeed(Constant.tokenAuthenticationCache, TokenGenerator.getSeed(userId));
		return true;
	}

	@Override
	public Map<String, Object> getBaseUserInfo(String userId) {
		Map<String,Object> userMap = userMapper.selectByUserId(userId);
		if (userMap != null && userMap.containsKey("userType")) {
			Integer userType = (Integer)userMap.get("userType");
			User user = new User();
			user.setUserId(userId);
			user.setUserType(userType);
//			if (userType == Constant.personUserType) {
//				return userMapper.selectBaseUserInfoByUserId(user);
//			}else if (userType == Constant.companyUserType) {
				return userMapper.selectBaseUserInfoByUserId(user);
//			}
		}
		return null;
	}

	@Override
	public Certification getCertification(String userId) {
		return certificationMapper.selectByPrimaryKey(userId);
	}

	@Override
	public Map<String, Object> addCertification(Certification cer) {
		Certification oldCer = certificationMapper.selectByPrimaryKey(cer.getUserId());
		if(oldCer != null){
//			   if(oldCer.getAuthStatus() ==2){//已经审核通过
//				   return MapConvert.getMap(ResultType.SUCCESS);
//			   }else{
				   certificationMapper.updateByPrimaryKeySelective(cer);
				   return MapConvert.getMap(ResultType.SUCCESS);
//			   }
		}else{
			certificationMapper.insertSelective(cer);
			return MapConvert.getMap(ResultType.SUCCESS);
		}
	}
}
